package com.ruoyi.web.controller.product;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.ProductClassify;
import com.ruoyi.product.service.IProductClassifyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 产品分类Controller
 *
 * @author ruoyi
 * @date 2020-12-27
 */
@Controller
@RequestMapping("/product/productClassify")
public class ProductClassifyController extends BaseController {
    private String prefix = "product/productClassify";

    @Autowired
    private IProductClassifyService productClassifyService;

    @RequiresPermissions("product:productClassify:view")
    @GetMapping()
    public String productClassify() {
        return prefix + "/productClassify";
    }

    /**
     * 查询产品分类树列表
     */
    @RequiresPermissions("product:productClassify:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ProductClassify> list(ProductClassify productClassify) {
        List<ProductClassify> list = productClassifyService.selectProductClassifyList(productClassify);
        return list;
    }

    /**
     * 导出产品分类列表
     */
    @RequiresPermissions("product:productClassify:export")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductClassify productClassify) {
        List<ProductClassify> list = productClassifyService.selectProductClassifyList(productClassify);
        ExcelUtil<ProductClassify> util = new ExcelUtil<ProductClassify>(ProductClassify.class);
        return util.exportExcel(list, "productClassify");
    }

    /**
     * 新增产品分类
     */
    @GetMapping(value = {"/add/{keyId}", "/add/"})
    public String add(@PathVariable(value = "keyId", required = false) Long keyId, ModelMap mmap) {
        if (StringUtils.isNotNull(keyId)) {
            mmap.put("productClassify", productClassifyService.selectProductClassifyById(keyId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存产品分类
     */
    @RequiresPermissions("product:productClassify:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductClassify productClassify) {
        return toAjax(productClassifyService.insertProductClassify(productClassify));
    }

    /**
     * 修改产品分类
     */
    @GetMapping("/edit/{keyId}")
    public String edit(@PathVariable("keyId") Long keyId, ModelMap mmap) {
        ProductClassify productClassify = productClassifyService.selectProductClassifyById(keyId);
        mmap.put("productClassify", productClassify);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品分类
     */
    @RequiresPermissions("product:productClassify:edit")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductClassify productClassify) {
        return toAjax(productClassifyService.updateProductClassify(productClassify));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:productClassify:remove")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{keyId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("keyId") Long keyId) {
        return toAjax(productClassifyService.deleteProductClassifyById(keyId));
    }

    /**
     * 选择产品分类树
     */
    @GetMapping(value = {"/selectProductClassifyTree/{keyId}", "/selectProductClassifyTree/"})
    public String selectProductClassifyTree(@PathVariable(value = "keyId", required = false) Long keyId, ModelMap mmap) {
        if (StringUtils.isNotNull(keyId)) {
            mmap.put("productClassify", productClassifyService.selectProductClassifyById(keyId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载产品分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = productClassifyService.selectProductClassifyTree();
        return ztrees;
    }
}
